package com.sys.web.back.controller;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.users.pojo.ADepart;
import com.sys.users.pojo.ADepartEnum;
import com.sys.users.pojo.APosition;
import com.sys.users.pojo.APositionEnum;
import com.sys.users.pojo.Customer;
import com.sys.users.service.IUsersDbService;


/**
 * 	管理员首页操作
 * 	@author 梦缠绵
 */
@Controller
@RequestMapping("/back/users/")
public class UsersBackController extends BaseController
{
	@Resource
	private IUsersDbService usersDbService;

	/**
	 * 用户列表
	 * @return
	 */
	@RequestMapping("/usersList")
	public String usersList(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		/*-------自定义搜索条件-------*/
		String positionId = request.getParameter("positionId");
		if(positionId == null)
		{
			positionId = "" ;
		}
		condMap.put("positionId", positionId);
		request.setAttribute("positionId", positionId);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<Customer> response = this.usersDbService.findCondListUsersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		model.addAttribute("response", response.toJSON());
		
		/* 查询所有的职位 */
		condMap.clear();
		/* 只查询启用的 */
		condMap.put("status", APositionEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<APosition> positionApiResponse = this.usersDbService.findCondListPositionService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("positionList", positionApiResponse.toJSON());
		return "/back/users/usersList";
	}
	
	/**
	 * 打开添加用户页面
	 * @return
	 */
	@RequestMapping("/usersInsert")
	public String usersInsert(HttpServletRequest request,String updateFlag,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsert--");
		/* 查询所有的职位 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 只查询启用的 */
		condMap.put("status", APositionEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<APosition> response = this.usersDbService.findCondListPositionService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("positionList", response.toJSON());
		return "/back/users/usersInsert";
	}
	
	/**
	 * 添加用户提交操作
	 * @return
	 */
	@RequestMapping(value = "/usersInsertSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String usersInsertSubmit(HttpServletRequest request, String birthdayStr , Customer users, Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
		/* 为对象赋值 */
		users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
		/* 为对象赋值 */
		/* 保存结果 */
		ApiResponse response = this.usersDbService.saveOneUsersService(users);
		ConstatFinalUtil.SYS_LOGGER.info("--用户添加返回结果:{}--",response.toJSON());
		return response.toJSON().toJSONString();
	}
	
	/**
	 * 用户查看
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(String id,Model model,HttpServletRequest request)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInfo--");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<Customer> response = this.usersDbService.findOneUsersService(condMap);
		/* 存储request */
		model.addAttribute("response", response.toJSON());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("update".equalsIgnoreCase(operType))
		{
			condMap.clear();
			/* 只查询启用的 */
			condMap.put("status", APositionEnum.STATUS_ENABLE.getStatus());
			/* 查询数据库
			 * 所有的数据以JSON的形式返回
			 *  */
			ApiResponse<APosition> apiResponseDb = this.usersDbService.findCondListPositionService(null, condMap);
			/* 将结果存储到Request中 */
			request.setAttribute("positionList", apiResponseDb.toJSON());
			
			return "/back/users/usersUpdate";
		}
		return "/back/users/usersInfo";
	}
	
	/**
	 * 添加用户提交操作
	 * @return
	 */
	@RequestMapping(value = "/usersUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String usersUpdateSubmit(HttpServletRequest request, String birthdayStr , Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if(id == null)
		{
			id = "" ; 
		}
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<Customer> response = this.usersDbService.findOneUsersService(condMap);
		Customer users = response.getDataOneJava() ; 
		/* 存储request */
		model.addAttribute("response", response.toJSON());
		
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if("update".equalsIgnoreCase(operType))
		{
			String status = request.getParameter("status");
			String positionId = request.getParameter("positionId");
			
			users.setStatus(Byte.valueOf(status));
			users.setPositionId(Integer.valueOf(positionId));
			
			/* 为对象赋值 */
			users.setBirthday(this.dateFormatUtil.strDate(birthdayStr));
			
			users.setUpdateTime(new Date());
			
			ApiResponse<Object> apiResponse = this.usersDbService.updateOneUsersService(users);
			return apiResponse.toJSON().toJSONString();
		}
		return response.toJSON().toJSONString();
	}
	
	/*====部门表开始====*/
	/**
	 * 部门列表页面
	 * @return
	 */
	@RequestMapping("/departList")
	public String departList(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--departList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ADepart> response = this.usersDbService.findCondListDepartService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", response.toJSON());
		return "/back/users/departList";
	}

	/**
	 * 打开添加部门页面
	 * @return
	 */
	@RequestMapping("/departInsert")
	public String departInsert(HttpServletRequest request,String updateFlag,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--departInsert--");
		return "/back/users/departInsert";
	}
	
	/**
	 * 添加部门提交操作
	 * @return
	 */
	@RequestMapping(value = "/departInsertSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String departInsertSubmit(HttpServletRequest request, String pubTimeStr , ADepart depart, Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--departInsertSubmit--");
		/* 为对象赋值 */
		depart.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
		depart.setCreateTime(new Date());
		depart.setUpdateTime(new Date());
		/* 保存结果 */
		ApiResponse response = this.usersDbService.saveOneDepartService(depart);
		ConstatFinalUtil.SYS_LOGGER.info("--部门添加返回结果:{}--",response.toJSON());
		return response.toJSON().toJSONString();
	}
	
	/**
	 * 打开更新部门页面
	 * @return
	 */
	@RequestMapping("/departUpdate")
	public String departUpdate(HttpServletRequest request,String id,
			Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--departUpdate--");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<ADepart> response = this.usersDbService.findOneDepartService(condMap);
		/* 存储request */
		request.setAttribute("response", response.toJSON());
		
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/back/users/departUpdate";
		}
		return "/back/users/departInfo";
	}
	
	/**
	 * 添加部门提交操作
	 * @return
	 */
	@RequestMapping(value = "/departUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String departUpdateSubmit(HttpServletRequest request,String id,
			Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--departUpdateSubmit--");
		ApiResponse<Object> apiResponseRes = new ApiResponse<Object>();
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<ADepart> response = this.usersDbService.findOneDepartService(condMap);
		/* 获取java对象 */
		ADepart depart = response.getDataOneJava() ;
		
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 接收参数 */
			String name = request.getParameter("name");
			String code = request.getParameter("code");
			String status = request.getParameter("status");
			String pubTimeStr = request.getParameter("pubTimeStr");
			String content = request.getParameter("content");
			/* 设置属性 */
			depart.setName(name);
			depart.setCode(code);
			depart.setContent(content);
			depart.setStatus(Byte.valueOf(status));
			
			depart.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
			depart.setUpdateTime(new Date());
			/* 数据库操作 */
			ApiResponse dbRes = this.usersDbService.updateOneDepartService(depart);
			ConstatFinalUtil.SYS_LOGGER.info("--部门添加返回结果:{}--",dbRes.toJSON());
			return dbRes.toJSON().toJSONString(); 
		}
		return apiResponseRes.toJSON().toJSONString();
	}
	
	/**
	 * 部门批量操作
	 * @return
	 */
	@RequestMapping(value = "/departBatch" , produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String departBatch(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--departBatch--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 从session中获取信息 */
		String[] ids = request.getParameterValues("ids");
		
		/* 操作类型 */
		String operType = request.getParameter("operType");
		if(ids != null)
		{
			int totalNum = ids.length ; 
			int succedNum = 0 ; 
			int failedNum = 0 ; 
			for (int i = 0; i < ids.length; i++)
			{
				String id = ids[i];
				if("status".equalsIgnoreCase(operType))
				{
					String status = request.getParameter("status");
					
					condMap.clear();
					condMap.put("id", id);
					ApiResponse<ADepart> apiResponseDb = this.usersDbService.findOneDepartService(condMap);
					ADepart depart = apiResponseDb.getDataOneJava();
					/* 设置状态 */
					depart.setStatus(Byte.valueOf(status));
					depart.setPubTime(new Date());
					
					ApiResponse<Object> departClassResponse = this.usersDbService.updateOneDepartService(depart);
					if(departClassResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
					{
						succedNum ++ ; 
					}else
					{
						failedNum ++ ; 
					}
				}
			}
			
			StringBuffer sb = new StringBuffer();
			sb.append("总条数:" + totalNum + ";");
			sb.append("成功条数:" + succedNum + ";");
			sb.append("失败条数:" + failedNum + ";");
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			apiResponse.setInfo(sb.toString(), Collections.EMPTY_MAP);
		}
		return apiResponse.toJSON().toJSONString();
	}
	/*====部门表结束====*/
	
	/*====职位表开始====*/
	/**
	 * 职位列表页面
	 * @return
	 */
	@RequestMapping("/positionList")
	public String positionList(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--positionList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/*-------自定义搜索条件-------*/
		String departId = request.getParameter("departId");
		if(departId == null)
		{
			departId = "" ;
		}
		condMap.put("departId", departId);
		request.setAttribute("departId", departId);
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<APosition> response = this.usersDbService.findCondListPositionService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", response.toJSON());
		
		/* 查询所有的部门 */
		condMap.clear();
		/* 只查询启用的 */
		condMap.put("status", ADepartEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ADepart> departResponse = this.usersDbService.findCondListDepartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("departList", departResponse.toJSON());
		
		return "/back/users/positionList";
	}

	/**
	 * 打开添加职位页面
	 * @return
	 */
	@RequestMapping("/positionInsert")
	public String positionInsert(HttpServletRequest request,String updateFlag,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--positionInsert--");
		/* 查询所有的部门 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 只查询启用的 */
		condMap.put("status", ADepartEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ADepart> response = this.usersDbService.findCondListDepartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("departList", response.toJSON());
		return "/back/users/positionInsert";
	}
	
	/**
	 * 添加职位提交操作
	 * @return
	 */
	@RequestMapping(value = "/positionInsertSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String positionInsertSubmit(HttpServletRequest request, String pubTimeStr , APosition position, Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--positionInsertSubmit--");
		/* 为对象赋值 */
		position.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
		position.setCreateTime(new Date());
		position.setUpdateTime(new Date());
		/* 保存结果 */
		ApiResponse response = this.usersDbService.saveOnePositionService(position);
		ConstatFinalUtil.SYS_LOGGER.info("--职位添加返回结果:{}--",response.toJSON());
		return response.toJSON().toJSONString();
	}
	
	/**
	 * 打开更新职位页面
	 * @return
	 */
	@RequestMapping("/positionUpdate")
	public String positionUpdate(HttpServletRequest request,String id,
			Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--positionUpdate--");
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<APosition> response = this.usersDbService.findOnePositionService(condMap);
		/* 存储request */
		request.setAttribute("response", response.toJSON());
		
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 查询所有的部门 */
			condMap.clear();
			/* 只查询启用的 */
			condMap.put("status", ADepartEnum.STATUS_ENABLE.getStatus());
			/* 查询数据库
			 * 所有的数据以JSON的形式返回
			 *  */
			ApiResponse<ADepart> departApiResponse = this.usersDbService.findCondListDepartService(null, condMap);
			/* 将结果存储到Request中 */
			request.setAttribute("departList", departApiResponse.toJSON());
			return "/back/users/positionUpdate";
		}
		return "/back/users/positionInfo";
	}
	
	/**
	 * 添加职位提交操作
	 * @return
	 */
	@RequestMapping(value = "/positionUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String positionUpdateSubmit(HttpServletRequest request,String id,
			Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--positionUpdateSubmit--");
		ApiResponse<Object> apiResponseRes = new ApiResponse<Object>();
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<APosition> response = this.usersDbService.findOnePositionService(condMap);
		/* 获取java对象 */
		APosition position = response.getDataOneJava() ;
		
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			/* 接收参数 */
			String departId = request.getParameter("departId");
			String name = request.getParameter("name");
			String code = request.getParameter("code");
			String status = request.getParameter("status");
			String pubTimeStr = request.getParameter("pubTimeStr");
			String content = request.getParameter("content");
			/* 设置属性 */
			position.setDepartId(Integer.valueOf(departId));
			position.setName(name);
			position.setCode(code);
			position.setContent(content);
			position.setStatus(Byte.valueOf(status));
			
			position.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
			position.setUpdateTime(new Date());
			/* 数据库操作 */
			ApiResponse dbRes = this.usersDbService.updateOnePositionService(position);
			ConstatFinalUtil.SYS_LOGGER.info("--职位添加返回结果:{}--",dbRes.toJSON());
			return dbRes.toJSON().toJSONString(); 
		}
		return apiResponseRes.toJSON().toJSONString();
	}
	
	/**
	 * 职位批量操作
	 * @return
	 */
	@RequestMapping(value = "/positionBatch" , produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String positionBatch(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--positionBatch--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 从session中获取信息 */
		String[] ids = request.getParameterValues("ids");
		
		/* 操作类型 */
		String operType = request.getParameter("operType");
		if(ids != null)
		{
			int totalNum = ids.length ; 
			int succedNum = 0 ; 
			int failedNum = 0 ; 
			for (int i = 0; i < ids.length; i++)
			{
				String id = ids[i];
				if("status".equalsIgnoreCase(operType))
				{
					String status = request.getParameter("status");
					
					condMap.clear();
					condMap.put("id", id);
					ApiResponse<APosition> apiResponseDb = this.usersDbService.findOnePositionService(condMap);
					APosition position = apiResponseDb.getDataOneJava();
					/* 设置状态 */
					position.setStatus(Byte.valueOf(status));
					position.setPubTime(new Date());
					
					ApiResponse<Object> positionClassResponse = this.usersDbService.updateOnePositionService(position);
					if(positionClassResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
					{
						succedNum ++ ; 
					}else
					{
						failedNum ++ ; 
					}
				}
			}
			
			StringBuffer sb = new StringBuffer();
			sb.append("总条数:" + totalNum + ";");
			sb.append("成功条数:" + succedNum + ";");
			sb.append("失败条数:" + failedNum + ";");
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			apiResponse.setInfo(sb.toString(), Collections.EMPTY_MAP);
		}
		return apiResponse.toJSON().toJSONString();
	}
	/*====职位表结束====*/
}